﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Facebook_Graph_Toolkit.FacebookObjects;

namespace Facebook_Graph_Toolkit {
    /// <summary>
    /// The Dialog class contains methods that return urls of Facebook dialogs
    /// </summary>
    public static class Dialog {
        /// <summary>
        /// Gets the url that prompts the user to send application requests
        /// </summary>
        /// <param name="AppConfig">Facebook Application Config. Sepcify null to use the settings in web.config</param>
        /// <param name="Message">The message the receiving user will see. Must not exceed 255 characters</param>
        /// <param name="TargetID">A user ID or username who must be a friend of the sender. If this is specified, the user will not have a choice of recipients. If this is omitted, the user will see a friend selector and will be able to select a maximum of 50 recipients.</param>
        /// <param name="Filter">Specifies which users will appear in the friend selector</param>
        /// <param name="RedirectAddress">An address relative to the Canvas Page address (http://apps.facebook.com/xxx/). User will be redirected to this address after the dialog.</param>
        /// <param name="Display">How the dialog page is intended to be displayed.</param>
        /// <param name="FriendSelectorTitle">The title of the friend selector. Must not exceed 50 characters.</param>
        /// <param name="TrackingData">Tracking Data which is stored in the AppRequest object on Facebook.</param>
        /// <returns>A url the user can be redirected to send application requests</returns>
        public static string GetAppRequestUrl(FacebookAppConfig AppConfig, string RedirectAddress, DialogDisplayType Display, AppRequestFilter Filter, string FriendSelectorTitle, string TargetID, string Message, string TrackingData) {
            if (AppConfig == null) {
                FacebookGraphToolkitConfiguration Config = Helpers.Common.GetAppConfig(true);
                AppConfig = new FacebookAppConfig();
                AppConfig.FacebookAppAddress = Config._FacebookAppAddress;
                AppConfig.FacebookAppID = Config._FacebookAppID;
            }

            IDictionary<string, string> parameters = new Dictionary<string, string>();
            if (Filter == AppRequestFilter.NonAppUsersOnly) parameters.Add("filters", "['app_non_users']");
            if (Filter == AppRequestFilter.AppUsersOnly) parameters.Add("filters", "['app_users']");
            if (Display == DialogDisplayType.FullPage) parameters.Add("display", "page");
            else parameters.Add("display", "popup");
            parameters.Add("app_id", AppConfig.FacebookAppID);
            parameters.Add("message", Message);
            parameters.Add("to", TargetID);
            parameters.Add("redirect_uri", AppConfig.FacebookAppAddress + RedirectAddress);
            parameters.Add("title", FriendSelectorTitle);
            parameters.Add("data", TrackingData);
            return Helpers.Common.BuildUrlQuery("http://www.facebook.com/dialog/apprequests", parameters);;
        }
        /// <summary>
        /// Gets the url that prompts the user to post to the wall
        /// </summary>
        /// <param name="AppConfig">Facebook Application Config. Sepcify null to use the settings in web.config</param>
        /// <param name="RedirectAddress">An address relative to the Canvas Page address (http://apps.facebook.com/xxx/). The user will be redirected to this address after the dialog.</param>
        /// <param name="PosterID">The ID or username of the user posting the message. If unspecified, default is current user. If specified, must be ID of the user or of a page that the user administers.</param>
        /// <param name="TargetID">The ID or username of the profile that this story will be published to. If unspecified, default is the value of <c>PosterID</c></param>
        /// <param name="PictureUrl">The Url to a picture attached to this post.</param>
        /// <param name="Source">The Url to a media file (e.g. SWF or video file) attached to this post. If both <c>source</c> and <c>picture</c> are specified, only <c>source</c> is used.</param>
        /// <param name="Link">A Link Attachment to this post.</param>
        /// <param name="Action">An action link that appears next to "Comment" and "Like".</param>
        /// <param name="Display">How the dialog page is intended to be displayed.</param>
        /// <returns>A url where the user can be redirected to post something on the wall</returns>
        public static string GetFeedDialogUrl(FacebookAppConfig AppConfig, string RedirectAddress, DialogDisplayType Display, string PosterID, string TargetID, string PictureUrl, string Source, LinkAttachment Link, PostAction Action){
            if (AppConfig == null) {
                FacebookGraphToolkitConfiguration Config = Helpers.Common.GetAppConfig(true);
                AppConfig = new FacebookAppConfig();
                AppConfig.FacebookAppAddress = Config._FacebookAppAddress;
                AppConfig.FacebookAppID = Config._FacebookAppID;
            }
            

            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("app_id", AppConfig.FacebookAppID);
            parameters.Add("redirect_uri", AppConfig.FacebookAppAddress+ RedirectAddress);
            if (Display == DialogDisplayType.FullPage) parameters.Add("display", "page");
            else parameters.Add("display", "popup");
            parameters.Add("from", PosterID);
            parameters.Add("to", TargetID);
            if (Link != null) {
                parameters.Add("link", Link.URL);
                parameters.Add("name", Link.Name);
                parameters.Add("caption", Link.Caption);
                parameters.Add("description", Link.Description);
            }
            parameters.Add("picture", PictureUrl);
            parameters.Add("source", Source);
            if (Action != null) {
                parameters.Add("actions", "[{'name':'" + Action.Name + "','link':'" + Action.Link + "'}]");
            }
            return Helpers.Common.BuildUrlQuery("http://www.facebook.com/dialog/feed", parameters);
        }

        /// <summary>
        /// Gets a url that prompts the user to add a friend. If the opposite side has already sent a friend request, this dialog prompts the user to confirm the friend request.
        /// </summary>
        /// <param name="AppConfig">Facebook Application Config. Sepcify null to use the settings in web.config</param>
        /// <param name="RedirectAddress">An address relative to the Canvas Page address (http://apps.facebook.com/xxx/). The user will be redirected to this address after the dialog.</param>
        /// <param name="Display">How the dialog page is intended to be displayed.</param>
        /// <param name="TargetID">The UserID or Username of whom to be added as friend</param>
        /// <returns>A Url to redirect the user to add another user as a friend</returns>
        public static string GetAddFriendDialogUrl(FacebookAppConfig AppConfig, string RedirectAddress, DialogDisplayType Display, string TargetID) {
            if (AppConfig == null) {
                FacebookGraphToolkitConfiguration Config = Helpers.Common.GetAppConfig(true);
                AppConfig = new FacebookAppConfig();
                AppConfig.FacebookAppAddress = Config._FacebookAppAddress;
                AppConfig.FacebookAppID = Config._FacebookAppID;
            }

            Dictionary<string, string> Parameters = new Dictionary<string, string>();

            if (Display == DialogDisplayType.FullPage) Parameters.Add("display", "page");
            else Parameters.Add("display", "popup");

            Parameters.Add("app_id", AppConfig.FacebookAppID);
            Parameters.Add("redirect_uri", AppConfig.FacebookAppAddress + RedirectAddress);
            Parameters.Add("id", TargetID);

            return Helpers.Common.BuildUrlQuery("http://www.facebook.com/dialog/friends", Parameters);
        }

        /// <summary>
        /// Gets a url that prompts the user to send a message
        /// </summary>
        /// <param name="AppConfig">Facebook Application Config. Sepcify null to use the settings in web.config</param>
        /// <param name="RedirectAddress">An address relative to the Canvas Page address (http://apps.facebook.com/xxx/). The user will be redirected to this address after the dialog.</param>
        /// <param name="Display">How the dialog page is intended to be displayed.</param>
        /// <param name="TargetID">A UserID or Username to which to send the message</param>
        /// <param name="PictureUrl">The Url of the picture that is displayed next to the link, if a link is provided. If unspecified, a picture will be automatically picked by Facebook and displayed next to the link.</param>
        /// <param name="Link">A link attached to this message. Note that the Caption property of the LinkAttachment object is ignored.</param>
        /// <returns>A url the user can be redirected to send a message</returns>
        public static string GetSendDialogUrl(FacebookAppConfig AppConfig, string RedirectAddress, DialogDisplayType Display, string TargetID, string PictureUrl, LinkAttachment Link) {
            if (AppConfig == null) {
                FacebookGraphToolkitConfiguration Config = Helpers.Common.GetAppConfig(true);
                AppConfig = new FacebookAppConfig();
                AppConfig.FacebookAppAddress = Config._FacebookAppAddress;
                AppConfig.FacebookAppID = Config._FacebookAppID;
            }

            Dictionary<string, string> Parameters = new Dictionary<string, string>();

            if (Display == DialogDisplayType.FullPage) Parameters.Add("display", "page");
            else Parameters.Add("display", "popup");

            Parameters.Add("app_id", AppConfig.FacebookAppID);
            Parameters.Add("redirect_uri", AppConfig.FacebookAppAddress + RedirectAddress);
            Parameters.Add("to", TargetID);
            if (Link != null) {
                Parameters.Add("link", Link.URL);
                Parameters.Add("name", Link.Name);
                Parameters.Add("description", Link.Description);
            }
            Parameters.Add("picture", PictureUrl);
            return Helpers.Common.BuildUrlQuery("http://www.facebook.com/dialog/send", Parameters);
        }
    }
    /// <summary>
    /// Enumeration of filter settings of an Facebook Application Request
    /// </summary>
    public enum AppRequestFilter {
        /// <summary>
        /// All friends of the current user will appear in the friend selector
        /// </summary>
        All,
        /// <summary>
        /// Only those friends who are using the application will appear in the friend selector
        /// </summary>
        AppUsersOnly,
        /// <summary>
        /// Only those friends who are not using the application will appear in the friend selector
        /// </summary>
        NonAppUsersOnly
    }

    /// <summary>
    /// Enumeration of Facebook Dialogs display types
    /// </summary>
    public enum DialogDisplayType {
        /// <summary>
        /// Renders a full page dialog
        /// </summary>
        FullPage,
        /// <summary>
        /// Reders a dialog to be used in browser popup
        /// </summary>
        Popup
    }
}
